package com.tinyideatech.generator.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JavaBeanUtils implements Serializable {

    private static Connection con = null;

    static class Ora {
        static final String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
        static final String DATABASE_URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        static final String DATABASE_USER = "databi";
        static final String DATABASE_PASSWORD = "123456";
    }

    static class MySql {
        static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
        static final String DATABASE_URL = "jdbc:mysql://rm-2ze8lxt7y44y5928no.mysql.rds.aliyuncs.com/etiriutf8?useUnicode=true&characterEncoding=UTF-8&useSSL=true";
        static final String DATABASE_USER = "root";
        static final String DATABASE_PASSWORD = "WWW123c0m";
    }

    public static Connection getOracleConnection() {
        try {
            Class.forName(Ora.DRIVER_CLASS);
            con = DriverManager.getConnection(Ora.DATABASE_URL, Ora.DATABASE_USER, Ora.DATABASE_PASSWORD);
            return con;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return con;
    }

    public static Connection getMySqlConnection() {
        try {
            Class.forName(MySql.DRIVER_CLASS);
            con = DriverManager.getConnection(MySql.DATABASE_URL, MySql.DATABASE_USER, MySql.DATABASE_PASSWORD);
            return con;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return con;
    }

    public static List<Map> getOracleTable(String Table) throws SQLException {
        getMySqlConnection();
        List<Map> list = new ArrayList<Map>();
        try {
            DatabaseMetaData m_DBMetaData = con.getMetaData();
            ResultSet colrs = m_DBMetaData.getColumns(null, MySql.DATABASE_USER.toUpperCase(), Table.toUpperCase(), "%");
            while (colrs.next()) {
                Map map = new HashMap();
                String columnName = colrs.getString("COLUMN_NAME");
                String columnType = colrs.getString("TYPE_NAME");
                int datasize = colrs.getInt("COLUMN_SIZE");
                int digits = colrs.getInt("DECIMAL_DIGITS");
                int nullable = colrs.getInt("NULLABLE");
                String remarks = colrs.getString("REMARKS");
                map.put("columnName", columnName);
                map.put("columnType", columnType);
                map.put("datasize", datasize);
                map.put("remarks", remarks);
                list.add(map);
//                System.out.println("TABLE_CAT" + "===" + colrs.getString("TABLE_CAT"));
//                System.out.println("TABLE_SCHEM" + "===" + colrs.getString("TABLE_SCHEM"));
//                System.out.println("TABLE_NAME" + "===" + colrs.getString("TABLE_NAME"));
//                System.out.println("COLUMN_NAME" + "===" + colrs.getString("COLUMN_NAME"));
//                System.out.println("DATA_TYPE" + "===" + colrs.getString("DATA_TYPE"));
//                System.out.println("TYPE_NAME" + "===" + colrs.getString("TYPE_NAME"));
//                System.out.println("COLUMN_SIZE" + "===" + colrs.getString("COLUMN_SIZE"));
//                System.out.println("BUFFER_LENGTH" + "===" + colrs.getString("BUFFER_LENGTH"));
//                System.out.println("DECIMAL_DIGITS" + "===" + colrs.getString("DECIMAL_DIGITS"));
//                System.out.println("NUM_PREC_RADIX" + "===" + colrs.getString("NUM_PREC_RADIX"));
//                System.out.println("NULLABLE" + "===" + colrs.getString("NULLABLE"));
//                System.out.println("REMARKS" + "===" + colrs.getString("REMARKS"));
//                System.out.println("COLUMN_DEF" + "===" + colrs.getString("COLUMN_DEF"));
//                System.out.println("SQL_DATA_TYPE" + "===" + colrs.getString("SQL_DATA_TYPE"));
//                System.out.println("SQL_DATETIME_SUB" + "===" + colrs.getString("SQL_DATETIME_SUB"));
//                System.out.println("CHAR_OCTET_LENGTH" + "===" + colrs.getString("CHAR_OCTET_LENGTH"));
//                System.out.println("ORDINAL_POSITION" + "===" + colrs.getString("ORDINAL_POSITION"));
//                System.out.println("IS_NULLABLE" + "===" + colrs.getString("IS_NULLABLE"));
            }
//            while(colRet.next()){
//                System.out.print("列名："+colRet.getString("COLUMN_NAME"));
//                System.out.print("  数据类型是："+colRet.getString("DATA_TYPE"));
//                System.out.print("  类型名称是："+colRet.getString("TYPE_NAME"));
//                System.out.print("  列大小是："+colRet.getString("COLUMN_SIZE"));
//                System.out.println("  注释是："+colRet.getString("REMARKS"));
//            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
        return list;
    }

    /**
     * 把Oracle字段类型 转化为 java类型
     *
     * @param sqlType 字段类型
     * @param size    字段大小
     * @param scale   默认=0
     * @return
     */
    public static String oracleSqlType2JavaType(String sqlType, int size, int scale) {
        System.out.println(sqlType);
        if (sqlType.equals("tinyint") || sqlType.equals("smallint")) {
            return "Integer";
        } else if (sqlType.indexOf("bigint") > -1) {
            return "Long";
        } else if (sqlType.indexOf("blob") > -1) {
            return "byte[]";
        } else if (sqlType.equals("float")
                || sqlType.equals("float precision")
                || sqlType.equals("double")
                || sqlType.equals("double precision")
                ) {
            return "Double";
        } else if (sqlType.equals("number")
                || sqlType.equals("decimal")
                || sqlType.equals("numeric")
                || sqlType.equals("real")) {
            return scale == 0 ? (size < 10 ? "Integer" : "Long") : "Double";
        } else if (sqlType.equals("varchar")
                || sqlType.equals("varchar2")
                || sqlType.equals("char")
                || sqlType.equals("nvarchar")
                || sqlType.equals("nchar")) {
            return "String";
        } else if (sqlType.equals("datetime")
                || sqlType.equals("date")
                || sqlType.equals("timestamp")) {
            return "Date";
        }
        return "String";
    }

    public static String getItems(List<Map> map, String tablename) {
        //记得转化成小写
        StringBuffer sb = new StringBuffer();
        sb.append("package com.tinyideatech.common.entity;");
        sb.append("\r\n");
        sb.append("import java.util.Date;\r\n");
        sb.append("\r\n");
        sb.append("@Data\r\n");
        sb.append("public class " + getUpperOne(UnderlineToHump(tablename.toLowerCase())) + "  implements java.io.Serializable  {\r\n");
        //得到私有属性
        for (Map map0 : map) {
            String columnname = map0.get("columnName").toString();
            String columntype = map0.get("columnType").toString();
            String columnsize = map0.get("datasize").toString();
            String javaType = oracleSqlType2JavaType(columntype.toLowerCase(), Integer.parseInt(columnsize), 0);
            String temp = "\tprivate " + javaType + " " + UnderlineToHump(columnname.toLowerCase()) + ";\r\n";
            sb.append(temp);
        }
        //得到getter和setter
//        for (Map map0 : map) {
//            String columnname = map0.get("columnName").toString();
//            String columntype = map0.get("columnType").toString();
//            String columnsize = map0.get("datasize").toString();
//            String javaType = oracleSqlType2JavaType(columntype.toLowerCase(),Integer.parseInt(columnsize),0);
//            String temp = "\tpublic "+javaType+" "+"get"+getUpperOne(columnname.toLowerCase())+"(){\r\n";
//            String temp1 = "\t\treturn "+columnname.toLowerCase()+";\r\n";
//            String temp2 = "\t}\r\n";
//            sb.append(temp+temp1+temp2);
//            temp = "\tpublic void "+"set"+getUpperOne(columnname.toLowerCase())+"("+javaType+" "+columnname.toLowerCase()+"){\r\n";
//            temp1 = "\t\tthis."+columnname.toLowerCase()+" = "+columnname.toLowerCase()+";\r\n";
//            temp2 = "\t}\r\n";
//            sb.append(temp+temp1+temp2);
//        }
        sb.append("}");
        return sb.toString();

    }

    /**
     * 把输入字符串的首字母改成大写
     *
     * @param str
     * @return
     */
    public static String getUpperOne(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static String UnderlineToHump(String para) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split("_");
        for (String s : a) {
            if (!para.contains("_")) {
                result.append(s);
                continue;
            }
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }

    public static void main(String[] args) throws IOException {

        //JavaBeanUtils.sysoutOracleTCloumns("pexam_items_title", "his_yhkf");
        try {
            String tables = "article, article_child, channels, code, issue, label_relation, log, publish_addition, publish_arcrel, recorder,url";
            String[] arr = tables.split(",");
            for (String str : arr) {
                str = str.trim();
                String name = getUpperOne(UnderlineToHump(str.toLowerCase()));
                List<Map> map = getOracleTable(str);
                String a = getItems(map, str);
                File file = new File("D:\\bbb\\" + name + ".java");
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(file, true);//true表示在文件末尾追加
                fos.write(a.getBytes());
                fos.close();//流要及时关闭
            }
            System.out.println("生成java完成");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
